package com.sc.nft.controller;

import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson.JSONObject;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.annotation.WithoutSign;
import com.sc.nft.dto.*;
import com.sc.nft.dto.hna.HnaFastPayReq;
import com.sc.nft.helper.HnaPayHelper;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.UserHnaExtService;
import com.sc.nft.sup.Result;
import com.sc.nft.util.HttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/pay/hnaPay")
@Api(tags = "新生接口")
public class HnaPayController {
  private final UserHnaExtService userHnaExtService;
  private final HnaPayHelper hnaPayHelper;

  @PostMapping("/account")
  @ApiOperation("开户")
  @UserLogin
  public Result<String> createAccount(@RequestBody @Valid UserHnaAccountCreateDTO dto) {
    return Result.success(userHnaExtService.createAccount(UserPool.getUser(), dto));
  }

  @PostMapping("/fastPay")
  @ApiOperation("快捷支付下单")
  @UserLogin
  public Result<String> fastPay(@RequestBody @Valid HnaFastPayReq req, HttpServletRequest request) {
    return Result.fail("请下载最新版本APP");

  }

  @PostMapping("/mallFastPay")
  @ApiOperation("ip商城快捷支付下单")
  @UserLogin
  public Result<String> mallFastPay(
      @RequestBody @Valid HnaFastPayReq req, HttpServletRequest request) {
    req.setIp(ServletUtil.getClientIP(request));
    return Result.success(userHnaExtService.mallFastPay(UserPool.getUser(), req));
  }

  @PostMapping("/fastPayConfirm")
  @ApiOperation("快捷支付确认")
  @UserLogin
  public Result<String> fastPayConfirm(
      HttpServletRequest request, @RequestBody @Valid HnaFastPayConfirmDTO dto) {
    dto.setMerUserIp(ServletUtil.getClientIP(request));
    return Result.success(userHnaExtService.fastPayConfirm(UserPool.getUser(), dto));
  }

  @PostMapping("/bindCard")
  @ApiOperation("提现绑卡")
  @UserLogin
  public Result<Long> bindCard(HttpServletRequest request, @RequestBody @Valid HnaCardBindDTO dto) {
    dto.setMerUserIp(ServletUtil.getClientIP(request));
    return Result.success(userHnaExtService.bindCard(UserPool.getUser(), dto));
  }

  @PostMapping("/bindCardConfirm")
  @ApiOperation("提现绑卡确认")
  @UserLogin
  public Result<String> bindCardConfirm(
      HttpServletRequest request, @RequestBody @Valid HnaCardBindConfirmDTO dto) {
    dto.setMerUserIp(ServletUtil.getClientIP(request));
    return Result.success(userHnaExtService.bindCardConfirm(UserPool.getUser(), dto));
  }

  @PostMapping("/fastPayNotify")
  @ApiIgnore
  @WithoutSign
  public String fastPayNotify(HttpServletRequest request) {
    return userHnaExtService.fastPayNotify(HttpUtil.getFormData(request));
  }

  @PostMapping("/withdraw")
  @ApiOperation("提现")
  @UserLogin
  public Result<String> withdraw(
      HttpServletRequest request, @RequestBody @Valid HnaWithdrawDto dto) {
    dto.setMerUserIp(ServletUtil.getClientIP(request));
    userHnaExtService.withdraw(UserPool.getUser(), dto);
    return Result.success();
  }

  @PostMapping("/discard")
  @UserLogin
  @ApiOperation("解绑提现卡")
  public Result<String> discard(@RequestBody @Valid HnaDiscardDto dto) {
    return Result.success(hnaPayHelper.discard(UserPool.getUser(), dto));
  }

  @PostMapping("/withdrawNotify")
  @ApiIgnore
  @WithoutSign
  public String withdrawNotify(HttpServletRequest request) {
    return userHnaExtService.withdrawNotify(HttpUtil.getFormData(request));
  }

  @PostMapping("/refundNotify")
  @ApiIgnore
  @WithoutSign
  public String refundNotify(HttpServletRequest request) {
    return userHnaExtService.refundNotify(HttpUtil.getFormData(request));
  }

  @GetMapping("/userInfo")
  @UserLogin
  public Result<JSONObject> userInfo() {
    return Result.success(userHnaExtService.userInfo(UserPool.getUser()));
  }


}
